package eworldpacs.ui;

import leadtools.dicom.DicomTag;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DicomTableAdapter extends ArrayAdapter<DicomTag> {
   Context context;
   double[] columnWidthRatios;
   String[] headerRowTitles;
   String[] stringValueBucket;
   int count;
   int columns;
   int containerWidth;
   DicomTableTextRetriever textRetreiver;
   
   public static DicomTableAdapter Create(Context context, int elementViewResourceId, int containerWidth, int count, int columns, double[] columnWidthRatios, String[] columnTitles){
      assert (columns == columnWidthRatios.length) && (columns == columnTitles.length);
      DicomTableAdapter adapter = new DicomTableAdapter(context, elementViewResourceId, null);
      adapter.setCount(count);
      adapter.setNumColumns(columns);
      adapter.setColumnWidthRatios(columnWidthRatios);
      adapter.setColumnTitles(columnTitles);
      adapter.setContainerWidth(containerWidth);
      return adapter;
   }
   private DicomTableAdapter(Context context, int elementViewResourceId, DicomTag[] values) {
       super(context, elementViewResourceId, new DicomTag[1]);
       this.context = context;
     }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      View result = convertView;
      
      if(result == null){
         LinearLayout row = new LinearLayout(context);
         row.setBackgroundColor(Color.DKGRAY);
         for (int i = 0; i < columns; ++i){
            TextView textView = new TextView(context);
            textView.setTextColor(Color.WHITE);
            textView.setTextSize(20);
            textView.setBackgroundColor(Color.TRANSPARENT);
            textView.setWidth((int)(this.containerWidth * this.columnWidthRatios[i]));
            if (i == 0)
               textView.setGravity(Gravity.LEFT);
            else
               textView.setGravity(Gravity.RIGHT);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            row.addView(textView, params);
         }
         
         result = row;
      }
      String[] textValues = this.textRetreiver.getStringsForRow(position);
      assert textValues.length == columns;
      LinearLayout row = (LinearLayout) result;
      for (int j = 0; j < columns; ++j){
         ((TextView)row.getChildAt(j)).setText(textValues[j]);
      }
      return result;
   }
  
   @Override
   public boolean isEnabled(int position){
      return false;
   }
  
   @Override
   public int getCount(){
      return this.count;
   }
   
   public View getCustomHeaderView(){
      LinearLayout row = new LinearLayout(context);
      row.setBackgroundColor(Color.BLACK);
      for (int i = 0; i < columns; ++i){
         TextView textView = new TextView(context);
         textView.setTextColor(Color.WHITE);
         textView.setTextSize(15);
         textView.setBackgroundColor(Color.TRANSPARENT);
         textView.setWidth((int)(this.containerWidth * this.columnWidthRatios[i]));
         textView.setText(this.headerRowTitles[i]);
         if (i == 0)
            textView.setGravity(Gravity.LEFT);
         else
            textView.setGravity(Gravity.RIGHT);
         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
         row.addView(textView, params);
      }
      return row;
   }
   
   private void setContainerWidth(int containerWidth){
      this.containerWidth = containerWidth;
   }
   private void setCount(int count){
      this.count = count;
   }
   private void setNumColumns(int columns){
      this.columns = columns;
   }
   private void setColumnWidthRatios(double[] ratios){
      this.columnWidthRatios = ratios;
   }
   
   private void setColumnTitles(String[] titles){
      this.headerRowTitles = titles;
   }
   
   public void setTextRetriever(DicomTableTextRetriever textRetriever){
      this.textRetreiver = textRetriever;
   }
     
}
